package com.yc.role.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.yc.api.CommonResult;
import com.yc.base.BaseController;
import com.yc.role.entity.RoleMenuRelation;
import com.yc.role.service.IRoleMenuRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台角色和菜单关系表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-12-09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/roleMenuRelation")
@Api(value = "RoleMenuRelationController", tags = "后台角色和菜单关系表")
public class RoleMenuRelationController extends BaseController {

    private final IRoleMenuRelationService roleMenuRelationService;

    @PostMapping("/add")
    @ApiOperation(value = "创建RoleMenuRelation", notes = "创建RoleMenuRelation")
    public CommonResult<?> create(@RequestBody RoleMenuRelation roleMenuRelation) {
        roleMenuRelation.setId(null);
        boolean isOk = roleMenuRelationService.create(roleMenuRelation);
        if (isOk) {
            return CommonResult.success(roleMenuRelation);
        }
        return CommonResult.failed();
    }

    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量创建RoleMenuRelation", notes = "批量创建RoleMenuRelation")
    public CommonResult<?> create() {
        JSONObject params = getParams();
        Long roleId = params.getLong("roleId", null);
        List<RoleMenuRelation> roleMenuRelationList = params.getBeanList("roleMenuRelationList", RoleMenuRelation.class);
        if (ObjectUtil.isNull(roleId) || roleMenuRelationList.isEmpty()) {
            return CommonResult.validateFailed();
        }
        boolean isOk = roleMenuRelationService.createBatch(roleId, roleMenuRelationList);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "删除RoleMenuRelation", notes = "删除RoleMenuRelation")
    public CommonResult<?> deleteByIds(@RequestBody List<Long> ids) {
        boolean isOk = roleMenuRelationService.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @ApiOperation(value = "更新RoleMenuRelation", notes = "更新RoleMenuRelation")
    public CommonResult<?> updateById(@PathVariable("id") long id, @RequestBody RoleMenuRelation roleMenuRelation) {
        roleMenuRelation.setId(id);
        boolean isOk = roleMenuRelationService.update(roleMenuRelation);
        if (isOk) {
            return CommonResult.success(roleMenuRelation);
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取RoleMenuRelation列表", notes = "获取RoleMenuRelation列表")
    public CommonResult<Object> listAll(@RequestBody JSONObject params) {
        Long roleId = params.getLong("roleId", null);
        if (ObjectUtil.isNull(roleId)) {
            return CommonResult.validateFailed();
        }
        List<RoleMenuRelation> list = roleMenuRelationService.selectList(roleId);
        return CommonResult.success(list);
    }
}
